package welsir.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import welsir.entity.dos.Article;
import welsir.entity.dos.ArticleClass;
import welsir.entity.vo.ArticleClassPageVO;
import welsir.mapper.ArticleClassMapper;
import welsir.mapper.ArticleMapper;
import welsir.service.IArticleClassService;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @author Genius
 * @date 2023/03/07 21:20
 **/
@Service
public class IArticleClassServiceImpl extends ServiceImpl<ArticleClassMapper, ArticleClass> implements IArticleClassService {

    @Autowired
    private ArticleClassMapper articleClassMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public ArticleClassPageVO GetList(Integer page, Integer limit) {
        List<ArticleClass> result = new ArrayList<>();

        Page<ArticleClass> articleClassPage = new Page<>(page, limit);

        ArticleClassPageVO classPageVO = new ArticleClassPageVO();

        QueryWrapper<ArticleClass> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        Page<ArticleClass> resultPage = this.articleClassMapper.selectPage(articleClassPage, wrapper);
        List<ArticleClass> articleClasses = resultPage.getRecords();

        for (ArticleClass articleClass : articleClasses) {
            Integer id = articleClass.getId();
            QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sort_class", id);
            Integer count = articleMapper.selectCount(queryWrapper);
            articleClass.setNum(count);
            result.add(articleClass);
        }

        long total = resultPage.getTotal();
        classPageVO.setData(result);
        classPageVO.setTotal(total);
        return classPageVO;
    }
}
